package dao.impl;

import dao.IWorkOrderDao;
import pojo.WorkOrder;
import util.DBHelper;
import util.PageHelper;
import vo.WorkOrderProject;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class WorkOrderImpl implements IWorkOrderDao {

    DBHelper db=new DBHelper();

    @Override
    public int insertWorkOrder(WorkOrder wo) {
        String sql="insert into workorder values(null,?,?,?,?,?)";
        Object[] objects={wo.getPid(),wo.getExecutor(),wo.getDescription(),wo.getOrderlevel(),wo.getCreateDate()};
        return db.dtUpdate(sql,objects);
    }

    @Override
    public int selectPageDataCount() {
        StringBuilder sql=new StringBuilder("select count(id) from workorder");
        Connection con=null;
        PreparedStatement ps=null;
        ResultSet resultSet=null;
        try {
            con = db.getCon();
            ps = con.prepareStatement(sql.toString());
            resultSet = ps.executeQuery();
            if (resultSet.next()) {
                return resultSet.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            db.closeAll(con,ps,resultSet);
        }
        return 0;
    }

    @Override
    public ArrayList<WorkOrderProject> selectIndexData(PageHelper ph) {
        String sql="select w.id,p.pname,w.executor,w.description,w.orderlevel,w.createdate " +
                "from workorder w inner join project p on w.pid=p.pid limit ?,?";
        int index=(ph.getPageIndex()-1)*ph.getShowNumber();
        return db.dtQuery(sql,WorkOrderProject.class,index,ph.getShowNumber());
    }
}
